﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using KaoQin.RFIDLIB;
using KaoQin.Common;
using KaoQin.DBUtility;

namespace KaoQin.RegisterCard
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private DispatcherTimer timer1;
        private bool isRuning = false;

        private void ShowContent(string content)
        {
            this.txtContent.Dispatcher.Invoke(new Action(() =>
            {
                if (txtContent.Text.Length>5000)
                {
                    txtContent.Text = string.Empty;
                }
                txtContent.Text +="["+DateTime.Now.ToString()+"]" +content + "\r\n";
                ; })); 
        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                timer1.Start();
                ShowContent("开即登记！");
                btnStop.IsEnabled = true;
                btnStart.IsEnabled = false;
                btnRead.IsEnabled = false;
            }
            catch (Exception ex)
            {
                ShowContent("开始登记出错了：" + ex.Message);
            }
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                timer1.Stop();
                ShowContent("停止登记！");
                btnStop.IsEnabled = false;
                btnStart.IsEnabled = true;
                btnRead.IsEnabled = true;
            }
            catch (Exception ex)
            {
                
               ShowContent("停止登记出错了：" + ex.Message);
            }
        }

        private void btnRead_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                int state;
                CardFun bll = new CardFun();
                Guid uniqueID;
                state = bll.GetCardUniqueID(ConfigHelper.NewPassword, out uniqueID);
                bll.DoBeed(50);
                if (state == 0)
                {
                    ShowContent("读取成功,唯一ID:" + uniqueID.ToString());
                }
                else
                {
                    ShowContent("读取失败：" + ConfigHelper.GetRFIDState(state));
                }

            }
            catch (Exception ex)
            {
                ShowContent("读取出错：" + ex.Message);
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //SQLiteHelper.ConnString = "Data Source=" + FileHelper.GetcurrPath() + "\\CardRegisterDB.db;Password=123;";
            btnStop.IsEnabled = false;
            init();
        }

        private void init()
        {
            timer1 = new DispatcherTimer();
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Interval = new TimeSpan(0, 0, 0, 0, 500);
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (!isRuning)
            {
                RegisterCard();
            }
        }

        private void RegisterCard()
        {
            isRuning = true;
            CardFun bll = new CardFun();
            int state;
            int state2;
            try
            {
                state = bll.ChangePass(string.Empty, ConfigHelper.NewPassword);
                if (state == 0)
                {
                    Guid uniqueID = Guid.NewGuid();
                    state2 = bll.WriteCardUniqueID(ConfigHelper.NewPassword, uniqueID);
                    bll.DoBeed(50);
                    if (state2 == 0)
                    {

                        ShowContent("登记成功,ID号是：" + uniqueID.ToString());
                    }
                    else
                    {
                        ShowContent("写入ID时：" + ConfigHelper.GetRFIDState(state));
                    }
                }
                else if (state == 8)
                {
                    //不用处理
                }
                else
                {
                    bll.DoBeed(50);
                    ShowContent("修改密码时：" + ConfigHelper.GetRFIDState(state));
                }


            }
            catch (Exception ex)
            {
                ShowContent("登记出错了：" + ex.Message);
            }
            finally
            {
                isRuning = false;
            }
        }
    }
}
